﻿using System;
using UnityEngine;

namespace IQIGame.Onigao.Game
{
    public class UITransformAdapter : MonoBehaviour
    {
        public enum UIAdapterType
        {
            [Tooltip("裁剪，缩放")]
            Cull = 0,

            [Tooltip("拉伸")]
            Stretch = 1,

            [Tooltip("视频裁剪适配")]
            MovieCull = 2,
        }

        [SerializeField]
        bool m_adaptOnAwake = true;

        public UIAdapterType adapterType = UIAdapterType.Stretch;

        [Tooltip("在最终的大小上进行大小的偏移")]
        [SerializeField]
        Vector2 sizeOffset = Vector2.zero;
        [HideInInspector]
        [SerializeField]
        bool onlyWidth = false;

        RectTransform rectTransform;

        void Awake()
        {
            rectTransform = transform as RectTransform;
            if (rectTransform != null)
            {
                Vector2 half = Vector2.one * 0.5f;
                rectTransform.anchorMin = half;
                rectTransform.anchorMax = half;
                rectTransform.anchoredPosition = Vector2.zero;
            }

            if (m_adaptOnAwake)
                AdaptBgUI();

            this.AddListeners();
        }

        [ContextMenu(nameof(AdaptBgUI))]
        public void AdaptBgUI()
        {
            if (rectTransform != null && ManagerCenter.UI != null)
            {
                Vector2 size;
                switch (adapterType)
                {
                    case UIAdapterType.Cull:
                        size = ManagerCenter.UI.BackgroundCullSize;
                        break;
                    case UIAdapterType.Stretch:
                        size = rectTransform.sizeDelta;
                        if (onlyWidth)
                            size.x = ManagerCenter.UI.BackgroundStretchSize.x;
                        else
                            size = ManagerCenter.UI.BackgroundStretchSize;
                        break;
                    case UIAdapterType.MovieCull:
                        size = ManagerCenter.UI.MovieCullSize;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }

                size += sizeOffset;
                rectTransform.sizeDelta = size;
            }
        }

        void AddListeners()
        {
            //MsgDispatcher.AddListener(MsgEventType.OnScreenSizeChanged, OnScreenSizeChanged);
            if (ManagerCenter.UI)
            {
                ManagerCenter.UI.onScreenSizeChanged += OnScreenSizeChanged;
            }
        }

        void OnScreenSizeChanged()
        {
            AdaptBgUI();
        }

        void RemoveListeners()
        {
            //MsgDispatcher.RemoveListener(MsgEventType.OnScreenSizeChanged, OnScreenSizeChanged);
            var uiMgr = ManagerCenter.UI;
            if (uiMgr != null)
            {
                uiMgr.onScreenSizeChanged -= OnScreenSizeChanged;
            }
        }

        protected virtual void OnDestroy()
        {
            this.RemoveListeners();
        }
    }

}
